package com.example.day14;

import android.app.Service;
import android.content.ComponentName;
import android.content.Intent;
import android.content.ServiceConnection;
import android.os.IBinder;
import android.support.v7.app.AppCompatActivity;
import android.os.Bundle;
import android.util.Log;
import android.view.View;
import android.widget.Button;

public class Main2Activity extends AppCompatActivity {
    private final String TAG=MainActivity.class.getSimpleName();
    MyService myService;
    Button button1,button2,button3,button4,button5,button6;
    private MyService.MyBinder binder;
    private ServiceConnection connection=new ServiceConnection() {
        @Override
        public void onServiceConnected(ComponentName name, IBinder binner) {
            binder =(MyService.MyBinder)binner;
            myService= (binder).getService();
            Log.e(TAG+"####21", "onServiceDisconnected: Connected");
        }
        @Override
        public void onServiceDisconnected(ComponentName name) {
            Log.e(TAG+"####2", "onServiceDisconnected: disconnect");
            binder=null;
        }
    };



    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_main);
        button1 = findViewById(R.id.button1);
        button2 = findViewById(R.id.button2);
        button3 = findViewById(R.id.button3);
        button4 = findViewById(R.id.button4);
        button5 = findViewById(R.id.button5);
        button6 = findViewById(R.id.button6);
        button1.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                Intent startIntent = new Intent(Main2Activity.this, MyService.class);
                startService(startIntent);
            }
        });

        button2.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                Intent stopIntent = new Intent(Main2Activity.this, MyService.class);
                stopService(stopIntent);

//                停止一个started服务有两种方法：
//                （1）在外部使用stopService()
//                （2）在服务内部使用stopSelf()方法。
            }
        });

        button3.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                Intent intent =new Intent(Main2Activity.this,MyService.class);
                //参数一：intent意图  参数二：绑定服务连接  参数三：flag标记BIND_AUTO_CREATE 绑定的时候自动创建服务
                bindService(intent,connection, Service.BIND_AUTO_CREATE);
            }
        });
        button4.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                unbindService(connection);// 解除绑定，断开连接
            }
        });
        button5.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {

                binder.startDownload();
                myService.method1();
            }
        });
        button6.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
             finish();
            }
        });
    }





}
